﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Renren.Common;

namespace Renren.Controls
{
    public partial class MessageBox : UserControl
    {
        private double defaultOpacity;
        private string defaultText;
        public event EventHandler<MessageEventArgs> Send;

        public string Text
        {
            get { return this.MessageTextBox.Text; }
            set { this.MessageTextBox.Text = value; }
        }
        
        public MessageBox()
        {
            InitializeComponent();
            DataContext = App.LoginUser;

            this.Loaded += (sender, arg) =>
                {
                    this.MessageTextBox.Text = this.Text;
                    this.defaultOpacity = this.Opacity;
                    this.defaultText = this.Text;
                };

            this.MessageTextBox.KeyDown += (sender, arg) =>
            {
                if (arg.Key == Key.Enter)
                {
                    Send(this, new MessageEventArgs(this.MessageTextBox.Text));
                }
            };

            this.MessageTextBox.GotFocus += (sender, arg) =>
                {
                    this.Opacity = 1;
                    this.MessageTextBox.Foreground = new SolidColorBrush(Colors.Black);
                };

            this.MessageTextBox.LostFocus += (sender, arg) =>
                {
                    this.Opacity = this.defaultOpacity;
                    this.MessageTextBox.Text = this.defaultText;
                    this.MessageTextBox.Foreground = new SolidColorBrush(Colors.Gray);
                };
        }

        private void Image_Tap(object sender, GestureEventArgs e)
        {
            PhoneApplicationService.Current.State["UserPage"] = App.LoginUser.UserId;
            (App.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/UserPage.xaml", UriKind.Relative));
        }
    }
}
